<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!defaultAddress.id" class="none">您需要先添加收货地址才可提交订单。</div>
      <template v-else>
      <ul>
         <li><span>收<i/>货<i/>人：</span>{{ defaultAddress.receiver }}</li>
            <li><span>联系方式：</span>{{ defaultAddress.contact }}</li>
            <li><span>收货地址：</span>{{ defaultAddress.fullLocation + defaultAddress.address }}</li>
      </ul>
      <!-- 有地址传 地址 -->
      <a @click="addAddressBtn(defaultAddress)" href="javascript:;">修改地址</a>
      </template>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="dialogVisible = true">切换地址</XtxButton>
      <!-- 传（）传过去就是undefined  否则传的可能是事件对象 -->
      <XtxButton class="btn" @click="addAddressBtn()">添加地址</XtxButton>
    </div>
  </div>
  <!-- 切换地址 -->
     <XtxDialog title="切换收货地址" v-model="dialogVisible" >
      <div class="text item" v-for="item in addresses"
      :key="item.id"
      @click="changeAddress(item.id)"
       :class="{active: item.id === active}"
       >
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation + defaultAddress.address}}</li>
        </ul>
      </div>
      <template v-slot:footer>
        <XtxButton @click="dialogVisible = false"  type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="comfirmAddress" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>

  <!-- 编辑地址 -->
   <AddressEdit ref="addressEditCom" @success-edit="successEdit"></AddressEdit>
</template>
<script>
import AddressEdit from './address-edit.vue'
import { ref, inject, computed, watch } from 'vue'
export default {
  name: 'CheckoutAddress',
  components: { AddressEdit },
  props: {
    addresses: {
      type: Array,
      default: () => []
    }
  },
  emits: ['change'], // vue3中 声明定义自定义事件的
  setup (props, { emit }) {
    const dialogVisible = ref(false) // 切换地址框 隐藏
    const addressEditCom = ref(null) // 忘记点：设未null ，需要在结构中 设置v-if
    const data = inject('data')
    const addAddressBtn = (address) => {
      // 把默认收货地址传给 子组件 用于展示
      addressEditCom.value.open(address) // 结构传 过来的是 defaultAddress
    }
    // 编辑成功后 默认显示地址
    const successEdit = (form) => {
      const defaultAddress = props.addresses.find(item => item.id === form.id) // 编辑上传后，有id
      // 遍历传递过来的数据 赋值给默认显示数据
      for (const key in defaultAddress) {
        defaultAddress[key] = form[key]
      }
    }
    const defaultAddress = computed(() => {
      return props.addresses.find(item => item.isDefault === 0) || {} // 0为默认显示地址
    })

    // id 直接从页面显示的收货地址中获取，变化的时候也会自动监听重新获取
    watch(defaultAddress, (address) => {
      // 收集 提交订单接口所需的收货地址 id
      if (address) data.addressId = address.id // 所选的地址id
    }, { immediate: true })

    const active = ref(defaultAddress.value.id)
    const newAddress = ref(null) // 暂存放 切换后的新地址
    // 实现 点击切换地址框里的 点击那个 那个就高亮
    const changeAddress = (theid) => {
      active.value = theid
      newAddress.value = props.addresses.find(item => item.id === theid)
    }
    // 点击切换地址框确认
    const comfirmAddress = () => {
      console.log(newAddress.value)
      for (const key in defaultAddress) {
        defaultAddress[key] = newAddress[key]
      }
      dialogVisible.value = false
    }

    return { addressEditCom, addAddressBtn, defaultAddress, successEdit, dialogVisible, active, changeAddress, comfirmAddress }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
